package com.bls.cate.ui.activity.person;

import android.net.Uri;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bls.blslib.base.BaseActivity;
import com.bls.blslib.eventbus.Event;
import com.bls.cate.R;
import com.bls.cate.constant.EventBusCode;
import com.bls.cate.listener.AppBarStateChangeListener;
import com.bls.cate.model.LoginSpRes;
import com.bls.cate.ui.activity.attention.AttentionActivity;
import com.bls.cate.ui.activity.setting.SettingActivity;
import com.bls.cate.ui.widget.HeaderImgDialog;
import com.bls.cate.ui.widget.TitleView;
import com.bls.cate.utils.AccountUtil;
import com.bls.cate.utils.FrescoImageLoader;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/6.
 */

public class PersonActivity extends BaseActivity implements AppBarStateChangeListener.OnStateChangeListener, TitleView.OnTitleClickListener {

    private AppBarLayout appBar;
    private TitleView titleView;
    private CollapsingToolbarLayout ctlTop;
    private NestedScrollView nsvBottom;
    private TextView navFollow, navPartner, navCollect;
    private SimpleDraweeView ivHeader;
    private TextView tvName;
    private SimpleDraweeView ivImage;

    private PersonActivity mActivity;
    private LoginSpRes loginSpRes;


    @Override
    protected void initParams() {
        super.initParams();
        mActivity = (PersonActivity) getActivity();
        loginSpRes = AccountUtil.getStrLoginSp();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person;
    }

    @Override
    protected void initFvb() {
        titleView = findViewById(R.id.title_view);
        appBar = findViewById(R.id.app_bar);
        ctlTop = findViewById(R.id.ctl_top);
        ivHeader = findViewById(R.id.iv_header);
        tvName = findViewById(R.id.tv_name);
        nsvBottom = findViewById(R.id.nsv_bottom);
        navFollow = findViewById(R.id.nav_follow);
        navPartner = findViewById(R.id.nav_partner);
        navCollect = findViewById(R.id.nav_collect);
        ivImage = findViewById(R.id.iv_image);
    }

    @Override
    protected void initTitle() {
        titleView.setTitleViewBg(mActivity.getResources().getColor(R.color.colorTrans));
        titleView.titleBottomLine.setVisibility(View.GONE);
        titleView.titleLeftIv1.setVisibility(View.VISIBLE);
        titleView.titleRightIv.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initView() {
        navFollow.setText("我的关注");
        navPartner.setText("我的情书");
        navCollect.setText("更多");
        FrescoImageLoader.showImage(ivHeader, loginSpRes.getHeaderImgMiddle());
        tvName.setText(loginSpRes.getNickName());
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)
                .setUri(Uri.parse("res://" + AppUtils.getAppPackageName() + "/" + R.drawable.temp_gif))
                .build();
        ivImage.setController(draweeController);
    }

    @Override
    protected void initListener() {
        AppBarStateChangeListener appBarStateChangeListener = new AppBarStateChangeListener(mActivity, this);
        appBar.addOnOffsetChangedListener(appBarStateChangeListener);
        titleView.setTitleClickListener(this);
        setOnClickListener(ivHeader, navFollow, navPartner, navCollect);
    }

    @Override
    public void onStateChanged(AppBarLayout appBarLayout, AppBarStateChangeListener.State state) {
        setTitleState(state == AppBarStateChangeListener.State.COLLAPSED);
    }

    /**
     * 设置title状态
     *
     * @param isCollapsed 是否处于折叠状态
     */
    private void setTitleState(boolean isCollapsed) {
        titleView.titleMiddleText.setText(isCollapsed ? "个人资料" : "");
        titleView.titleLeftIv1.setImageResource(isCollapsed ? R.mipmap.icon_arrow_1 : R.mipmap.icon_arrow_1_white);
        titleView.titleRightIv.setImageResource(isCollapsed ? R.mipmap.icon_setting : R.mipmap.icon_setting_white);
        titleView.setTitleViewBg(isCollapsed ? mActivity.getResources().getColor(R.color.colorWhite) : mActivity.getResources().getColor(R.color.colorTrans));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_header:
                HeaderImgDialog headerImgDialog = new HeaderImgDialog(getActivity(), loginSpRes.getHeaderImgLarge());
                headerImgDialog.show();
                break;
            case R.id.nav_follow:
                ActivityUtils.startActivity(AttentionActivity.class);
                break;
            case R.id.nav_partner:
                ToastUtils.showShort(getString(R.string.to_be_continued));
                break;
            case R.id.nav_collect:
                ToastUtils.showShort(getString(R.string.to_be_continued));
                break;
        }
    }

    @Override
    public void onTitleClick(int btnType) {
        switch (btnType) {
            case 1:
                mActivity.finish();
                break;
            case 3:
                ActivityUtils.startActivity(SettingActivity.class);
                break;
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveStickyEvent(Event event) {
        super.receiveStickyEvent(event);
        if (event.getCode() == EventBusCode.ChangeUserInfo_Refresh) {
            loginSpRes = AccountUtil.getStrLoginSp();
            if (loginSpRes != null) {
                FrescoImageLoader.showImage(ivHeader, loginSpRes.getHeaderImgMiddle());
                tvName.setText(loginSpRes.getNickName());
            }
        }
    }
}
